# The Electoral Effects of State-Sponsored Anti-LGBTQ Measures

## Overview
This repository contains the data and code necessary to replicate the analyses in "The Electoral Effects of State-Sponsored Anti-LGBTQ Measures" by Violeta I. Haas, Konstantin Bogatyrev, Tarik Abou-Chadi, Heike Klüver and Lukas F. Stoetzer. 

## Software Requirements
- **R**: Version 4.0 or higher
- **Operating System**: macOS, Windows, or Linux
- **Memory**: Minimum 8GB RAM (16GB recommended for simulations)
- **Disk Space**: ~300KB for data and outputs
- **Required R Packages**: devtools, readxl, did, giscoR, ggthemes, purrr, ggplot2, ggpubr, lubridate, modelsummary, plm, randomizr, scales, dplyr, sf, synthdid, tidyverse, MCPanel, progress

## Instructions for Replication

### Prerequisites
1. Install R (version 4.0 or higher)
2. Install required R packages (see `code/00_run.R` or above)
3. Ensure sufficient disk space (~300KB for data and outputs)

### Step-by-Step Replication
1. **Clone/Download** this repository
2. **Run main script**: `source("code/00_run.R")`
   - This will load packages, clean data, run analyses, and generate outputs
   - **Note**: The main analyses script (`03_analyses.R`) is commented out by default as it includes computationally intensive permutation tests (10,000 simulations). To run the complete analyses, uncomment line 76 in `00_run.R` by removing the `#` before `source("03_analyses.R", local = TRUE)`

### Expected Runtime
- **Data preparation**: ~1 minute
- **Analyses (with simulations)**: >10 hours (depending on hardware)  
- **Figure/table generation**: ~14 minutes

**Total runtime**: > 10 hours (with analyses) or ~15 minutes (without analyses)


## File Structure

### Data Files (`/data`)

#### Raw Data (`/data/raw`)
- `2001_gmina_sejm.xls`: 2001 Sejm election results by municipality
- `2005_gmina_sejm.xls`: 2005 Sejm election results by municipality  
- `2007_gmina_sejm.xls`: 2007 Sejm election results by municipality
- `2011_gmina_sejm.xls`: 2011 Sejm election results by municipality
- `2015_gmina_sejm.xls`: 2015 Sejm election results by municipality
- `2019_gmina_sejm.csv`: 2019 Sejm election results by municipality
- `lgbt_free_zones.Rda`: Dataset of municipalities with anti-LGBTQ+ resolutions
- `unadopted_zones.Rda`: Dataset of municipalities that proposed but didn't adopt resolutions
- `parl_elections_2001-2019.Rda`: Cleaned and harmonized election data (2001-2019), generated in `/code/01_cleaning.R`

#### Processed Data (`/data/processed`)
- `LGBTQ_election_data.Rda`: Main analysis dataset (treatment includes only resolutions issued pre-2019-election), generated in `/code/02_merging.R`
- `LGBTQ_election_data_w1w2.Rda`: Extended dataset, needed for Table 19 only (treatment includes all resolutions), generated in `/code/02_merging.R`

#### Results Data (`/data/results`)
- `sdid_results.Rda`: Synthetic difference-in-differences estimation results, generated in `/code/03_analyses.R` (pre-save)
- `simulations.Rda`: Permutation test simulation results (10,000 iterations), generated in `/code/03_analyses.R` (pre-save)


### Code Files (`/code`)
- `00_run.R`: Master script that loads packages and runs all other scripts in sequence
- `01_cleaning.R`: Cleans and harmonizes parliamentary election data from 2001-2019
- `02_merging.R`: Merges election data with LGBTQ-free zones data and creates treatment indicators
- `03_analyses.R`: Runs synthetic difference-in-differences analyses and permutation tests


#### Functions (`/code/functions`)
- `fun_cutoff.R`: Contains function that creates a cutoff sample of municipalities within 50km of treatment-control boundaries
- `fun_sdid_custom_plot.R`: Contains a function for custom synthetic difference-in-differences visualizations in Figure 7
- `fun_estimator.R`: Contains function that implements the matrix completion estimator into the synthetic difference-in-differences function for Table 9
- `fun_table.R`: Contains function for generating LaTeX tables from analyses results

#### Plots (`/code/plots`)
- `01_figure1.R`: creates Figure 1. Distribution of municipalities adopting anti-LGBTQ resolutions before the 2019 election.
- `02_figure2.R`: creates Figure 2. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019
parliamentary election by sample.
- `03_figure5.R`: creates Figure 5. Distribution of municipalities with anti-LGBTQ resolutions by wave.
- `04_figure6.R`: creates Figure 6. Distribution of PiS vote share in the 2019 parliamentary election.
- `05_figure7.R`: creates Figure 7. Average overall, opposition, and government turnout (%) among municipalities for all parliamentary elections between 2001 and 2019 in treated, observed (unweighted) control, and synthetic control municipalities.
- `06_figure8.R`: creates Figure 8. Distribution of synthetic control weights for overall, opposition, and government turnout.
- `07_figure9.R`: creates Figure 9. Distribution of municipalities within 50 km distance from treatment-control boundaries.
- `08_figure10.R`: creates Figure 10. Effect of anti-LGBTQ resolutions on party-specific turnout and vote shares in the 2019 parliamentary election by sample.
- `09_figure11.R`: creates Figure 11. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election by sum of treatment levels.
- `10_figure12.R`: creates Figure 12. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election by sum of treatment resolutions.
- `11_figure13.R`: creates Figure 13. Distribution of municipalities with anti-LGBTQ resolutions in effect prior to the 2019 parliamentary election by government level passing the resolution.
- `12_figure14.R`: creates Figure 14. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the
2019 parliamentary election, separated by government unit passing the resolution.
- `13_figure15.R`: creates Figure 15. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election, separated by type of resolution.
- `14_figure16.R`: creates Figure 16. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout using the never-treated control unit pool.
- `15_figure17.R`: creates Figure 17. Density plot of sDiD ATT estimates from 10,000 simulations that randomly assigned municipalities to anti-LGBTQ resolutions.
- `16_figure18.R`: creates Figure 18. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding the ten largest municipalities with over 300,000 inhabitants.
- `17_figure19.R`: creates Figure 19. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding municipalities that proposed but did not adopt resolutions (i.e., discussed, rejected or dismissed proposals).


#### Tables (`/code/tables`)
- `01_table2.R`: creates Table 2. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sample.
- `02_table3.R`: creates Table 3. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on party-specific turnout and vote shares by sample.
- `03_table4.R`: creates Table 4. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sum of treatment levels.
- `04_table5.R`: creates Table 5. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sum of treatment resolutions.
- `05_table6.R`: creates Table 6. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, separated by government unit passing the resolution.
- `06_table7.R`: creates Table 7. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, separated by type of resolution.
- `07_table8.R`: creates Table 8. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout using the never-treated control unit pool.
- `08_table9.R`: creates Table 9. Estimated effects of anti-LGBTQ resolutions on overall, opposition, and government turnout
in treated municipalities in the 2019 parliamentary election by estimator.
- `09_table10.R`: creates Table 10. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding the ten largest municipalities with over 300,000 inhabitants.
- `10_table11.R`: creates Table 11. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding municipalities that proposed but did not adopt resolutions (i.e., discussed, rejected or dismissed proposals).
- `11_table12.R`: creates Table 12. Number of Municipalities by Treatment Status. 


### Output Files

#### Plots (`/output/plots`)
- `figure1.pdf`: Figure 1. Distribution of municipalities adopting anti-LGBTQ resolutions before the 2019 election.
- `figure2.pdf`: Figure 2. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019
parliamentary election by sample.
- `figure5.pdf`: Figure 5. Distribution of municipalities with anti-LGBTQ resolutions by wave.
- `figure6.pdf`: Figure 6. Distribution of PiS vote share in the 2019 parliamentary election.
- `figure7.pdf`: Figure 7. Average overall, opposition, and government turnout (%) among municipalities for all parliamentary elections between 2001 and 2019 in treated, observed (unweighted) control, and synthetic control municipalities.
- `figure8.pdf`: Figure 8. Distribution of synthetic control weights for overall, opposition, and government turnout.
- `figure9.pdf`: Figure 9. Distribution of municipalities within 50 km distance from treatment-control boundaries.
- `figure10.pdf`: Figure 10. Effect of anti-LGBTQ resolutions on party-specific turnout and vote shares in the 2019 parliamentary election by sample.
- `figure11.pdf`: Figure 11. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election by sum of treatment levels.
- `figure12.pdf`: Figure 12. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election by sum of treatment resolutions.
- `figure13.pdf`: Figure 13. Distribution of municipalities with anti-LGBTQ resolutions in effect prior to the 2019 parliamentary election by government level passing the resolution.
- `figure14.pdf`: Figure 14. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the
2019 parliamentary election, separated by government unit passing the resolution.
- `figure15.pdf`: Figure 15. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout in the 2019 parliamentary election, separated by type of resolution.
- `figure16.pdf`: Figure 16. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout using the never-treated control unit pool.
- `figure17.pdf`: Figure 17. Density plot of sDiD ATT estimates from 10,000 simulations that randomly assigned municipalities to anti-LGBTQ resolutions.
- `figure18.pdf`: Figure 18. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding the ten largest municipalities with over 300,000 inhabitants.
- `figure19.pdf`: Figure 19. Effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding municipalities that proposed but did not adopt resolutions (i.e., discussed, rejected or dismissed proposals).


#### Tables (`/output/tables`)
- `table2.txt`: creates Table 2. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sample.
- `table3.txt`: creates Table 3. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on party-specific turnout and vote shares by sample.
- `table4.txt`: creates Table 4. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sum of treatment levels.
- `table5.txt`: Table 5. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout by sum of treatment resolutions.
- `table6.txt`: Table 6. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, separated by government unit passing the resolution.
- `table7.txt`: Table 7. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, separated by type of resolution.
- `table8.txt`: Table 8. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout using the never-treated control unit pool.
- `table9.txt`: Table 9. Estimated effects of anti-LGBTQ resolutions on overall, opposition, and government turnout
in treated municipalities in the 2019 parliamentary election by estimator.
- `table10.txt`: Table 10. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding the ten largest municipalities with over 300,000 inhabitants.
- `table11.txt`: Table 11. Synthetic difference-in-differences estimates for the effect of anti-LGBTQ resolutions on overall, opposition, and government turnout, excluding municipalities that proposed but did not adopt resolutions (i.e., discussed, rejected or dismissed proposals).
- `table12.txt`: Table 12. Number of Municipalities by Treatment Status. 


## Log Files 
- `output/logs/session_info.Rda`: R session information for reproducibility
- `output/logs/session_details.Rda`: Detailed system and package information


## Package Citations

### Core Analysis Packages
- **synthdid**: Arkhangelsky, D. (2025). synthdid: Synthetic Difference-in-Difference Estimation. *R package version 0.0.9*, https://github.com/synth-inference/synthdid
- **did**: Callaway, B., & Sant'Anna, P. H. (2021). did: Difference in Differences *R package version 2.1.2*, https://bcallaway11.github.io/did/.
- **MCPanel**: Athey, S., Bayati, M., Doudchenko, N., Imbens, G., & Khosravi, K. (2017). MCPanel: Matrix Completion algorithms for Causal Panel Data Models. *R package version 0.0*, https://github.com/susanathey/MCPanel.

### Data Manipulation and Visualization
- **tidyverse**: Wickham, H., Averick, M., Bryan, J., Chang, W., McGowan, L. D., François, R., ... & Yutani, H. (2019). Welcome to the tidyverse. *Journal of Open Source Software*, 4(43), 1686.
- **dplyr**: Wickham, H., François, R., Henry, L., Müller, K., & Vaughan D. (2023). dplyr: A grammar of data manipulation. *R package version 1.1.4*, https://CRAN.R-project.org/package=dplyr.
- **ggplot2**: Wickham, H. (2016). ggplot2: Elegant graphics for data analysis. *Springer-Verlag New York*, https://ggplot2.tidyverse.org.
- **sf**: Pebesma, E. (2018). Simple features for R: Standardized support for spatial vector data. *The R Journal*, 10(1), 439-446.

### Statistical and Econometric Methods
- **plm**: Croissant, Y., & Millo, G. (2008). Panel data econometrics in R: The plm package. *Journal of Statistical Software*, 27(2), 1-43.
- **randomizr**: Coppock, A. (2023). randomizr: Easy-to-use tools for common forms of random assignment and sampling. *R package version 1.0.0*, https://CRAN.R-project.org/package=randomizr.
- **modelsummary**: Arel-Bundock, V. (2022). modelsummary: Data and model summaries in R. *Journal of Statistical Software*, 103(1), 1-23.

### Data Import and Processing
- **readxl**: Wickham, H., & Bryan, J. (2025). readxl: Read Excel files. *R package version 1.4.5*, https://CRAN.R-project.org/package=readxl.
- **lubridate**: Grolemund, G., & Wickham, H. (2011). Dates and times made easy with lubridate. *Journal of Statistical Software*, 40(3), 1-25.
- **progress**: Csárdi, G., & FitzJohn, R. (2018). progress: Terminal progress bars. *R package version 1.2.3*, https://CRAN.R-project.org/package=progress.
- **devtools**: Wickham, H., Hester, J., Chang, W., & Bryan, J. (2022). devtools: Tools to make developing R packages easier. *R package version 2.4.5*, https://CRAN.R-project.org/package=devtools.

### Geographic and Spatial Data
- **giscoR**: Hernangómez, D. (2023). giscoR: Download map data from GISCO Eurostat. *R package version 0.6.1*, https://ropengov.github.io/giscoR/.

### Additional Visualization
- **ggthemes**: Arnold, J. B. (2024). ggthemes: Extra themes, scales and geoms for 'ggplot2'. *R package version 5.1.0*, https://CRAN.R-project.org/package=ggthemes.
- **ggpubr**: Kassambara, A. (2023). ggpubr: 'ggplot2' based publication ready plots. *R package version 0.6.0*, https://CRAN.R-project.org/package=ggpubr. 
- **scales**: Wickham, H., Lin Pedersen T., & Seidel, D. (2022). scales: Scale functions for visualization. *R package version 1.4.0*, https://CRAN.R-project.org/package=scales.

### Functional Programming
- **purrr**: Henry, L., & Wickham, H. (2025). purrr: Functional programming tools. *R package version 1.0.4*, https://CRAN.R-project.org/package=purrr. 

## Data and Code Availability Statement
All data and code necessary for replication are included in this repository. The analysis can be fully reproduced using the provided scripts and data files.

## Contact
Violeta I. Haas  
https://violeta-haas.github.io/